<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/8/11
 * Time: 11:23
 */

namespace app\models;

use yii\base\Model;
use Yii;
use \common\helps\tools;//使用工具类


class Pilot extends Model
{

    /**
     * title 表名
     * @var string
     */
    protected $table = 'd_pilot';  //属性


    /**
     * title 添加示范应用
     * @return int
     * @throws \yii\db\Exception
     */
    public function addPilot($create_time,$uid)
    {
        $id = tools::createUuid();
        $field = ['id', 'create_time','user_id'];
        $batchData = [[$id, $create_time,$uid]];
        $add = Yii::$app->db->createCommand()->batchInsert(tools::databaseName().'.'.$this->table, $field, $batchData)->execute();
        if ($add) {
            return $id;
        } else {
            return 1;
        }
    }

    /**
     * title 保存示范应用
     * @param $id
     * @param $project_id
     * @param $project_name
     * @param $thesis_id
     * @param $article_name
     * @param $periodical_name
     * @param $create_time
     * @param $realpath
     * @param $author
     * @param $uid
     * @return int
     * @throws \yii\db\Exception
     */
    public function savePilot($id,$project_id,$project_name,$pilot_name,$location,$description,$create_time,$realpath,$uid,$status,$pilot_date)
    {

        $data = ['project_id' => $project_id, 'project_name' => $project_name, 'pilot_name' => $pilot_name, 'place' => $location, 'description' => $description, 'update_time' => $create_time, 'image' => $realpath, 'user_id' => $uid, 'status' => $status,'pilot_date'=>$pilot_date];
//        var_dump($data);die;
        $save = Yii::$app->db->createCommand()->update(tools::databaseName() . '.' .$this->table, $data, "id = '$id'")->execute();

        return $save;
    }



    /**
     * title 示范应用详情
     * @param $project_id
     * @return array
     */
    public function detailPilot($id)
    {
        $query = new \yii\db\Query();
        $list = $query->select('*')->from(tools::databaseName() . '.' .$this->table)->where(['id' => $id])->all();

        return $list;


    }


    /**
     * title 删除示范应用
     * @param $id
     * @return int
     * @throws \yii\db\Exception
     */
    public function delPilot($id)
    {
        $del = Yii::$app->db->createCommand()->delete(tools::databaseName() . '.' .$this->table, "id = '$id'")->execute();

        return $del;
    }


    /**
     * title 条件搜索
     * @param $uid
     * @param $type
     * @param $status
     * @return array
     */
    public function searchPilot($uid,$status)
    {
        $query = new \yii\db\Query();
            $where = ['user_id' => $uid, 'status' => $status];
        $list = $query->select('*')->from(tools::databaseName(). '.' .$this->table)->where($where)->all();

        return $list;


    }

}